-
Notifications
You must be signed in to change notification settings - Fork 4.6k
fix(structured outputs): resolve memory leak in parse methods #2860
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: next
Are you sure you want to change the base?
Conversation
a389776 to
690c4b1
Compare
690c4b1 to
d0c242a
Compare
|
memory leak tests are a bit flaky, tbh, that’s why I didn’t add it |
RobertCraigie
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does pydantic know what class to instantiate now that we aren't actually passing it?
| def clear_locals(string: str, *, stacklevel: int) -> str: | ||
| caller = get_caller_name(stacklevel=stacklevel + 1) | ||
| return string.replace(f"{caller}.<locals>.", "") | ||
| return re.sub(r"([A-Za-z_]\w*)\[[^\[\]]+\](?=\()", r"\1", string) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
q: what does this do? can you add a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahh right, I'll add a comment. It's stripping out the generic name
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added a comment, also I've fixed the same leak for streaming too
We’re already instantiating the appropriate class here |
|
ahhh |
This PR avoids using runtime generics with Pydantic, which can leak memory, by using a simpler implementation for parsing the response message.
It also fixes generic conflicts in the tests, which happen because of how Pydantic v1 and Pydantic v2 implement
__repr__.Leak results in the main
Leak results in #2860
Leak results in #2148
Running tests locally
To run the test locally, use the following PEP 723 compatible script:
Note that you should set the appropriate branch for the openai-python dependency in the script.
For the main branch, use:
openai @ git+https://github.com/openai/openai-python.git@main,